﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreLibrary.People
{
    public class LikeNotLike
    {
        public List<LikeNotLikeProperty> likeNotLike = new List<LikeNotLikeProperty>();

        public void AddProperty(TierLevel level, Type type, string propertyName, object propertyValue)
        {
            AddProperty(new LikeNotLikeProperty() { Level = level, Type = type, PropertyName = propertyName, PropertyValue = propertyValue });
        }

        public void AddProperty(LikeNotLikeProperty property)
        {
            likeNotLike.Add(property);
        }

        public IEnumerable<LikeNotLikeProperty> LikeNotLikeProperties
        {
            get
            {
                return likeNotLike;
            }
        }

        public TierLevel EvaluateItem(dynamic item)
        {
            var level = TierLevel.Neutral;
            foreach (var matchedItem in likeNotLike.Where(t => item.GetType().Equals(t.Type)))
            {
                var propertyInfo = item.GetType().GetProperty(matchedItem.PropertyName);
                if (propertyInfo.GetValue(item, null).Equals(matchedItem.PropertyValue))
                {
                    level = TierLevelHelper.AdjustTier(level, matchedItem.Level);
                }
            }
            return level;
        }

        public int EvaluatePersonBond(dynamic person)
        {
            int result = 0;
            foreach (var matchedItem in likeNotLike.Where(t => person.GetType().Equals(t.Type)))
            {
                var propertyInfo = person.GetType().GetProperty(matchedItem.PropertyName);
                if (propertyInfo.GetValue(person, null).Equals(matchedItem.PropertyValue))
                {
                    result += TierLevelHelper.GetTierValue(matchedItem.Level);
                }
            }
            return result;
        }
    }
}
